﻿/*
COPYRIGHT (C) 2010-2011 Raynald Messié

THIS FILE IS PART OF SCRUMPILOT.

SCRUMPILOT IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUMPILOT IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUMPILOT. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/

using System;
using System.Collections.Generic;
using Microsoft.TeamFoundation.WorkItemTracking.Client;

namespace ScrumPilot.TFSHelpers
{
    public partial class TFSServices
    {

        public List<WorkItem> GetWIListAll()
        {
            var qry = new TFSQuery(TfsWorkItemStore);
            return qry.GetAllWorkItems();
        }

        public List<WorkItem> GetWIList(String project, String currentSprint, Boolean setParent)
        {
            const string sQuery = "select * FROM WorkItems  where [System.TeamProject] = @project and [System.IterationPath] under @iterationPath and [System.State] <> 'Removed'  order by [ScrumPilot.Score.Precedence], [ScrumPilot.Score.Level], [Microsoft.VSTS.Scheduling.RemainingWork]";

            List<WorkItem> lst = null;
            if (!String.IsNullOrWhiteSpace(currentSprint))
            {
                var qry = new TFSQuery(TfsWorkItemStore);
                lst = qry.GetWorkItemList(sQuery, new Dictionary<string, String>
                                                      {
                                                          { "project", project },
                                                          { "iterationPath", currentSprint }
                                                      });
            }
            return lst;
        }

        public List<WorkItem> GetWIListUser(String project, String currentSprint, String assigneTo)
        {
            //            const string sQuery = "select * FROM WorkItems  where [System.TeamProject] = @project and [System.IterationPath] under @iterationPath and [System.WorkItemType] ='Task' and [System.AssignedTo]=@assigneTo and [System.State] <> 'Removed'  order by [ScrumPilot.Score.Precedence], [ScrumPilot.Score.Level], [Microsoft.VSTS.Scheduling.RemainingWork]";
            const string sQuery = "select * FROM WorkItems  where [System.TeamProject] = @project and [System.WorkItemType] ='Task' and [System.AssignedTo]=@assigneTo and [System.State] <> 'Removed'  order by [ScrumPilot.Score.Precedence], [ScrumPilot.Score.Level], [Microsoft.VSTS.Scheduling.RemainingWork]";

            var lst = new List<WorkItem>();
            if (!String.IsNullOrWhiteSpace(currentSprint))
            {

                var qry = new TFSQuery(TfsWorkItemStore);
                lst = qry.GetWorkItemList(sQuery, new Dictionary<string, String>
                                          {
                                              {"project", project},
                                              {"iterationPath", currentSprint},
                                              {"assigneTo", assigneTo}
                                          });
            }
            return lst;
        }

        public int CountActiveWorkItemsUser(String assignedTo, ref Nullable<DateTime> dMax)
        {
            int iRet = 0;
            if (TfsWorkItemStore != null)
            {
                var parameters = new Dictionary<string, String> { { "assignedTo", assignedTo } };
                const string sQuery = "select [System.ChangedDate] FROM WorkItems  where [System.WorkItemType] ='Task' and [System.ChangedBy]=@assignedTo and [System.State] <> 'Removed'";
                var qry = new TFSQuery(TfsWorkItemStore);

                WorkItemCollection retVal = qry.GetWorkItemCollection(sQuery, parameters);
                foreach (WorkItem wi in retVal)
                {
                    if (!dMax.HasValue || dMax < wi.ChangedDate)
                        dMax = wi.ChangedDate;
                }
                iRet = retVal.Count;
            }
            return iRet;
        }

        public WorkItem GetWorkItem(Int32 id, Boolean bEdit)
        {
            WorkItem wi = TfsWorkItemStore.GetWorkItem(id);
            if (bEdit)
                wi.Open();
            return wi;
        }

        public Boolean SaveWorkItem(WorkItem workItemToSave)
        {
            Boolean bRet = false;
            if (workItemToSave.IsOpen)
            {
                if (workItemToSave.IsDirty)
                {
                    //  var valid=workItemToSave.Validate(); // Use for debug, in case of
                    workItemToSave.Save();
                }
                workItemToSave.Close();
                bRet = true;
            }
            return bRet;
        }
    }
}